package org.example.medicalstaff.controller;


import com.alibaba.fastjson.JSONObject;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.example.medicalstaff.entity.Drugs;
import org.example.medicalstaff.entity.DrugsDetailed;
import org.example.medicalstaff.entity.DrugsTemplate;
import org.example.medicalstaff.service.impl.DrugsServiceImpl;
import org.example.medicalstaff.utils.DateStringFormat;
import org.example.medicalstaff.vo.DrugAndPreDetailed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/out_doctor")
public class DrugsController {
    @Autowired
    DrugsServiceImpl drugsService;
    /**
     * 新增西药模板
     * 测试URL:http://127.0.0.1:9090/out_doctor/west_medicine_medicine_template/add
     *         备注：
     *         测试结果：正常
     */
    @PostMapping("/west_medicine_medicine_template/add")
    public CommonResponse<DrugsTemplate> drugs_manageTemplate_add1(
            HttpServletRequest httpRequest
    ) { JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int Scope = 1;
        int UsersID=(int)jsonObject.get("UsersID");
        String Name =(String)jsonObject.get("Name");
        //设置当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = currentDateTime.format(formatter);
        String pattern = "yyyy-MM-dd HH:mm:ss";
        Date date = DateStringFormat.parseDateTimeString(formattedDateTime,pattern);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String CreationTime = sdf2.format(date);
        CommonResponse<DrugsTemplate> commonResponse =drugsService.medicine_manageTemplate_add(
                Name,Scope,UsersID,CreationTime
        );
        return commonResponse;
    }
    /**
     * 删除西药模板
     *      测试URL:http://127.0.0.1:9090/out_doctor/west_medicine_medicine_template/del/{name}
     *      备注：要带上两个参数
     *      测试结果：正常
     */
    @PostMapping("/west_medicine_medicine_template/del/{templateID}")
    public CommonResponse<String> drugs_manageTemplate_delete1(
            @PathVariable("templateID") int  templateID){
        CommonResponse<String> commonResponse =drugsService.medicine_manageTemplate_delete(templateID);
        return commonResponse;
    }
    /**
     * 修改西药模板
     *     测试URL:http://127.0.0.1:9090/out_doctor/west_medicine_medicine_template/del/{name}
     *    备注：Name,UserID,CreationTime是不可改的
     *    测试结果：正常
     */
    @PutMapping("/west_medicine_medicine_template/modify")
    public CommonResponse<DrugsTemplate> drugs_manageTemplate_update1(
            @RequestParam("Name") String Name,
            @RequestParam("Scope") int Scope,
            @RequestParam("UsersID") int UsersID,
            @RequestParam("CreationTime") String CreationTime,
            @RequestParam("DelMark") int delMark
    ){
        CommonResponse<DrugsTemplate> commonResponse=drugsService.medicine_manageTemplate_update(
                Name,Scope,UsersID,CreationTime,delMark
        );
        return commonResponse;
    }
    /**
     * 获取全部西药模板
     *      测试URL:http://127.0.0.1:9090/out_doctor/west_medicine_medicine_template/all
     *      备注：
     *      测试结果：正常
     */
    @GetMapping("/west_medicine_medicine_template/all")
    public CommonResponse<List<DrugsTemplate>> drugs_manageTemplate_getList(

    ){
        CommonResponse<List<DrugsTemplate>> commonResponse=drugsService.medicine_manageTemplate_getList();
        return commonResponse;
    }
    /**
     * 根据名称获得西药模板
     *  测试URL:http://127.0.0.1:9090/out_doctor/west_medicine_medicine_template/all
     *  备注：这是根据template表中的Name来get
     *  测试结果：正常
     */
    @GetMapping("/west_medicine_medicine_template/search/{keyword}")
    public CommonResponse<DrugsTemplate> drugs_manageTemplate_get1(
            @PathVariable("keyword")String name
    ){
        CommonResponse<DrugsTemplate> commonResponse=drugsService.medicine_manageTemplate_get(name);
        return commonResponse;
    }

    /**
     * 新增西药详情
     * 测试URL:http://127.0.0.1:9090/out_doctor/west_medicine_medicine_template/add_m
     *  备注：
     *  测试结果：正常
     */
    @PostMapping("/west_medicine_medicine_template/add_m")
    public CommonResponse<DrugsDetailed> drugs_manageDetailed_add1(
            HttpServletRequest httpRequest
    ){ JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int DrugsID=(int)jsonObject.get("DrugsID");
        int DrugsTempID=(int)jsonObject.get("DrugsTempID");
        String Dosage =(String)jsonObject.get("Dosage");
        String DrugsUsage =(String)jsonObject.get("DrugsUsage");
        String Frequency =(String)jsonObject.get("Frequency");

        CommonResponse<DrugsDetailed> commonResponse =drugsService.medicine_manageDetailed_add(
                DrugsID,DrugsTempID,Dosage,DrugsUsage,Frequency
        );
        return commonResponse;
    }
    /**
     * 删除西药模板中药品
     *  测试URL:http://127.0.0.1:9090/out_doctor/west_medicine_medicine_template/del_m/{DrugsID}
     *  备注：这里仅删除了drugs表里的
     *  测试结果：正常
     */
    @PostMapping("/west_medicine_medicine_template/del_m")
    public CommonResponse<DrugsDetailed> drugs_manageDrugs_delete(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        String DrugsName = (String) jsonObject.get("DrugsName");
        String format = (String) jsonObject.get("DrugsFormat");
        int DrugTemplateID = (int) jsonObject.get("DrugTemplateID");
        CommonResponse<DrugsDetailed> commonResponse =drugsService.medicine_manageDetailed_delete(DrugsName,format,DrugTemplateID);
        return commonResponse;
    }
    /**
     * 根据ID获得西药详情
     *      测试URL:http://127.0.0.1:9090/out_doctor/west_medicine_medicine_template/search/detail
     *      备注：
     *      测试结果：正常
     */
    @GetMapping("/west_medicine_medicine_template/search/detail")
    public CommonResponse<List<DrugAndPreDetailed>> check_manageRelation_get1(
            @RequestParam("DrugsTempID")int DrugsTempID
    ){
        CommonResponse<List<DrugAndPreDetailed>> commonResponse=drugsService.medicine_manageDetailed_getList1(DrugsTempID);
        return commonResponse;
    }



    /**
     * 修改西药详情
     *     测试URL:http://127.0.0.1:9090/out_doctor/west_medicine_medicine_template/modify_m
     *     备注：DrugsID和DrugsTempID不可修改
     *     测试结果：正常
     */
    @PutMapping("/west_medicine_medicine_template/modify_m")
    public CommonResponse<DrugsDetailed> check_manageTemplate_update1(
            @RequestParam("DrugsID") int DrugsID,
            @RequestParam("DrugsTempID") int DrugsTempID,
            @RequestParam("Dosage") String Dosage,
            @RequestParam("DrugsUsage") String DrugsUsage,
            @RequestParam("Frequency") String Frequency
    ){
        CommonResponse<DrugsDetailed> commonResponse=drugsService.medicine_manageDetailed_update(
                DrugsID,DrugsTempID,Dosage,DrugsUsage,Frequency
        );
        return commonResponse;
    }

}
